package gui2.g2;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;

public class ChatGUI extends JFrame {

	private JLabel lblCaption;

	private JLabel lblUsername;
	private JTextField txtUsername;
	private JButton btnStart;

	private JList list;
	private JScrollPane listScrollPane;

	private JTable table;

	private JTextField txtMsg;
	private JButton btnSendMsg;

	private ChatModel chatModel = new ChatModel();

	public ChatGUI() {
		setTitle("Chat");
		setDefaultCloseOperation(EXIT_ON_CLOSE);

		createWidgets();
		layoutWidgets();
		setupInteractions();

		setLocationRelativeTo(null);
		setMinimumSize(new Dimension(500, 500));
	}

	private void createWidgets() {
		lblCaption = new JLabel("CHAT");
		lblCaption.setFont(lblCaption.getFont().deriveFont(25F));

		lblUsername = new JLabel("Username:");
		txtUsername = new JTextField();
		btnStart = new JButton("Start Chat");
		btnStart.setPreferredSize(new Dimension(
				btnStart.getPreferredSize().width, 100));
		btnStart.setMinimumSize(btnStart.getPreferredSize());

		list = new JList();
		/*
		 * Bei der Nutzung eines JScrollPane darf für die Liste keine Größe
		 * festgelegt werden. Statt dessen sollte die Größe über die ScrollPane
		 * festgelegt sein.
		 */
		// list.setPreferredSize(new Dimension(500, 400));
		list.setBorder(BorderFactory.createLineBorder(Color.BLACK));

		ChatListModel clm = new ChatListModel();
		chatModel.addChatModelListener(clm);
		list.setModel(clm);
		list.setCellRenderer(new ChatListRenderer());

		listScrollPane = new JScrollPane(list);

		ChatTableModel ctm = new ChatTableModel();

		table = new JTable();
		table.setModel(ctm);

		chatModel.addChatModelListener(ctm);

		txtMsg = new JTextField();
		btnSendMsg = new JButton("Send");
	}

	private void layoutWidgets() {
		getContentPane().setLayout(new GridBagLayout());

		GridBagConstraints c = new GridBagConstraints();
		c.gridx = 0;
		c.gridy = 0;
		c.gridwidth = 3;
		c.gridheight = 1;
		getContentPane().add(lblCaption, c);

		c = new GridBagConstraints();
		c.gridx = 0;
		c.gridy = 1;
		c.gridwidth = 1;
		c.gridheight = 1;
		c.anchor = GridBagConstraints.WEST;
		getContentPane().add(lblUsername, c);

		getContentPane().add(txtUsername, new GridBagHelp(0, 2).hfill().done());

		getContentPane().add(
				btnStart,
				new GridBagHelp(0, 3).anchor(
						GridBagConstraints.FIRST_LINE_START).done());

		getContentPane().add(
				listScrollPane,
				new GridBagHelp(1, 1, 1, 3).bounds(5, 5, 5, 5).scale(1, 1)
						.fill().done());

		getContentPane().add(table,
				new GridBagHelp(2, 1, 1, 3).scale(1, 1).fill().done());

		getContentPane().add(txtMsg,
				new GridBagHelp(1, 5).scale(1, 0).fill().done());

		getContentPane().add(btnSendMsg,
				new GridBagHelp(2, 5).anchorRight().done());

		pack();
	}

	private void setupInteractions() {
		btnSendMsg.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				String txt = txtMsg.getText();
				chatModel.addMessage(txt);
				txtMsg.setText("");
				System.out.println(Thread.currentThread().getName());
			}
		});

		txtMsg.getDocument().addDocumentListener(new DocumentListener() {

			{
				updateSendButton();
			}

			@Override
			public void removeUpdate(DocumentEvent e) {
				updateSendButton();
			}

			@Override
			public void insertUpdate(DocumentEvent e) {
				updateSendButton();
			}

			@Override
			public void changedUpdate(DocumentEvent e) {
				updateSendButton();
			}

			private void updateSendButton() {
				String txt = txtMsg.getText();
				btnSendMsg.setEnabled(txt != null && !txt.trim().isEmpty());
			}

		});

		getContentPane().addMouseMotionListener(new MouseMotionListener() {

			@Override
			public void mouseMoved(MouseEvent e) {
				checkAfk(e);
			}

			@Override
			public void mouseDragged(MouseEvent e) {
				checkAfk(e);
			}

			private void checkAfk(MouseEvent e) {
				if (e.getX() <= 10
						&& e.getY() >= getContentPane().getSize().height - 10) {
					chatModel.addMessage("AFK");
				}
			}
		});
	}

	public static void main(String[] args) {

		SwingUtilities.invokeLater(new Runnable() {

			@Override
			public void run() {
				
			

				JFrame f = new ChatGUI();
				f.setVisible(true);
				
				JOptionPane.showMessageDialog(f, "Chat gestartet!");
			}
		});

	}
}
